<template>
  <div class="auth-container">
    <bk-animation></bk-animation>
    <div class="auth-box register-box">
      <div class="auth-header">
        <div class="logo-container">
          <img src="/logo.png" alt="Logo" class="logo" />
        </div>
        <h1 class="title">创建新账户</h1>
        <p class="subtitle">加入我们，开始您的旅程</p>
      </div>

      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" class="auth-form">
        <el-form-item prop="username">
          <div class="input-icon-wrapper">
            <el-icon class="input-icon">
              <User />
            </el-icon>
            <el-input v-model="registerForm.username" placeholder="请输入用户名" size="large" class="auth-input" />
          </div>
        </el-form-item>

        <el-form-item prop="email">
          <div class="input-icon-wrapper">
            <el-icon class="input-icon">
              <Message />
            </el-icon>
            <el-input v-model="registerForm.email" placeholder="请输入邮箱" size="large" class="auth-input" />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="input-icon-wrapper">
            <el-icon class="input-icon">
              <Lock />
            </el-icon>
            <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" size="large" show-password
              class="auth-input" />
          </div>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <div class="input-icon-wrapper">
            <el-icon class="input-icon">
              <Check />
            </el-icon>
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" size="large"
              show-password class="auth-input" />
          </div>
        </el-form-item>

        <div class="terms-container">
          <el-checkbox v-model="registerForm.agreeTerms">我已阅读并同意</el-checkbox>
          <router-link to="/other" class="terms-link">服务条款</router-link>和 <router-link to="/other"
            class="terms-link">隐私政策</router-link>
        </div>

        <el-button type="primary" size="large" class="auth-btn" :loading="loading" @click="handleRegister"
          :disabled="!registerForm.agreeTerms">
          立即注册
        </el-button>

        <div class="auth-footer">
          <span>已有账户？</span>
          <router-link to="/login" class="auth-link">立即登录</router-link>
        </div>
      </el-form>

      <div class="auth-social">
        <p class="social-divider"><span>或通过以下方式注册</span></p>
        <div class="social-buttons">
          <button class="social-button wechat">
            <i class="social-icon wechat-icon"></i>
          </button>
          <button class="social-button qq">
            <i class="social-icon qq-icon"></i>
          </button>
          <button class="social-button weibo">
            <i class="social-icon weibo-icon"></i>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Check } from '@element-plus/icons-vue'
import bkAnimation from '../components/bkAnimation.vue'

import { useRouter } from 'vue-router'
import network from '../api/index'

const router = useRouter()
const loading = ref(false)
const registerFormRef = ref(null)
const { communityApi,userApi,request } = network

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  agreeTerms: false
})

const validatePass = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 12, message: '长度在3到12个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return;
  try {
    await registerFormRef.value.validate();

    if (!registerForm.agreeTerms) {
      return ElMessage.warning('请阅读并同意服务条款和隐私政策');
    }

    loading.value = true;
    await request(userApi.register, registerForm, 'POST');
    ElMessage.success('注册成功');
    router.push('/login');
  } catch (error) {
    // 处理错误信息
    const errorMessage = error || error || '注册失败，请稍后重试';
    ElMessage.error(errorMessage);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
// 主题颜色
$primary-color: #66a6ff;
$primary-gradient: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);
$text-color: #2c3e50;
$text-secondary: #606266;
$border-color: #e5e7eb;
$background-color: white;
$shadow-color: rgba(0, 0, 0, 0.1);

.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  min-height: 90vh;
}

.auth-box {
  z-index: 1;
  width: 600px;
  /* 增加宽度 */
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  background-color: white;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 6px;
    background: $primary-gradient;
  }
}

.register-box {
  max-width: 650px;
  /* 增加最大宽度 */
}

.auth-header {
  text-align: center;
  margin-bottom: 30px;

  .logo-container {
    margin-bottom: 20px;

    .logo {
      height: 60px;
      object-fit: contain;
    }
  }

  .title {
    color: $text-color;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 10px;
  }

  .subtitle {
    color: $text-secondary;
    font-size: 16px;
  }
}

.auth-form {
  margin-bottom: 20px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 表单内容居中 */

  .el-form-item {
    width: 90%;
    /* 表单项宽度 */
    max-width: 500px;
    /* 最大宽度 */
  }
}

.input-icon-wrapper {
  position: relative;
  width: 100%;
  /* 确保宽度100% */

  .input-icon {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: $primary-color;
    z-index: 2;
    font-size: 18px;
  }

  .auth-input {
    width: 100%;
    /* 确保输入框宽度100% */

    :deep(.el-input__wrapper) {
      padding-left: 40px;
      border-radius: 12px;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
      transition: all 0.3s ease;
      height: 50px;
      /* 增加高度 */

      &:hover {
        box-shadow: 0 4px 12px rgba(102, 166, 255, 0.2);
      }

      &.is-focus {
        box-shadow: 0 0 0 2px rgba(102, 166, 255, 0.2);
      }
    }
  }
}

.terms-container {
  display: flex;
  align-items: center;
  justify-content: center;
  /* 居中 */
  flex-wrap: wrap;
  margin-bottom: 20px;
  font-size: 14px;
  color: $text-secondary;
  width: 90%;
  max-width: 500px;

  .terms-link {
    color: $primary-color;
    text-decoration: none;
    margin: 0 4px;

    &:hover {
      text-decoration: underline;
    }
  }
}

.auth-btn {
  width: 90%;
  max-width: 500px;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 2px;
  border: none;
  background-image: $primary-gradient;
  border-radius: 12px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 166, 255, 0.3);
  }

  &:active {
    transform: translateY(0);
  }
}

.auth-footer {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: $text-secondary;
  width: 100%;

  .auth-link {
    color: $primary-color;
    text-decoration: none;
    font-weight: 500;

    &:hover {
      text-decoration: underline;
    }
  }
}

.auth-social {
  margin-top: 20px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .social-divider {
    display: flex;
    align-items: center;
    color: $text-secondary;
    font-size: 14px;
    margin: 20px 0;
    width: 90%;
    max-width: 500px;

    &::before,
    &::after {
      content: '';
      flex: 1;
      height: 1px;
      background-color: $border-color;
    }

    span {
      padding: 0 15px;
    }
  }

  .social-buttons {
    display: flex;
    justify-content: center;
    gap: 20px;

    .social-button {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: none;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.3s ease;
      background-color: #f5f7fa;

      &:hover {
        transform: translateY(-3px);
        box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
      }

      .social-icon {
        width: 24px;
        height: 24px;
        background-size: contain;
        background-repeat: no-repeat;
        background-position: center;
      }

      &.wechat {
        &:hover {
          background-color: #07c160;

          .social-icon {
            filter: brightness(0) invert(1);
          }
        }

        .wechat-icon {
          background-image: url('../assets/img/icons/wechat.png');
        }
      }

      &.qq {
        &:hover {
          background-color: #12b7f5;

          .social-icon {
            filter: brightness(0) invert(1);
          }
        }

        .qq-icon {
          background-image: url('../assets/img/icons/qq.png');
        }
      }

      &.weibo {
        &:hover {
          background-color: #e6162d;

          .social-icon {
            filter: brightness(0) invert(1);
          }
        }

        .weibo-icon {
          background-image: url('../assets/img/icons/weibo.png');
        }
      }
    }
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .auth-box {
    width: 100%;
    padding: 30px 20px;
  }

  .auth-form {
    .el-form-item {
      width: 100%;
    }
  }

  .terms-container,
  .auth-btn,
  .social-divider {
    width: 100%;
  }
}

@media (max-width: 576px) {
  .auth-header {
    .title {
      font-size: 24px;
    }

    .subtitle {
      font-size: 14px;
    }
  }

  .terms-container {
    font-size: 12px;
  }

  .auth-btn {
    height: 46px;
    font-size: 15px;
  }

  .social-buttons {
    .social-button {
      width: 45px;
      height: 45px;
    }
  }
}
</style>